﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class _04_DataReader : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCommand_Click(object sender, EventArgs e)
        {
            List<Catagory> list = new List<Catagory>();
            SqlConnection conn = new SqlConnection(DBUnit.ConnStr);
            //获取数据库只读对象
            SqlDataReader sdr = DBUnit.GetSqlDataReader(conn);
            //循环读取每一条数据库记录
            while (sdr.Read())
            {
                Catagory catagory = new Catagory();
                catagory.CategoryID = (int)sdr["CategoryID"];
                catagory.Desn = sdr["Desn"] as string;
                catagory.ParentID = (int)sdr["ParentID"];
                if (sdr["OrderBy"] == null || sdr["OrderBy"].ToString() == "")
                {
                    catagory.OrderBy = 0;
                }
                else
                {
                    catagory.OrderBy = Convert.ToInt32(sdr["OrderBy"].ToString());
                }
                catagory.Remark = sdr["Remark"] as string;
                //将每一条记录添加到 泛型集合
                list.Add(catagory);
            }

            //必须关闭数据库连接,否则数据库使用将越来越慢，因为内存有泄漏 
            conn.Close();

            //设置DataSource数据源
            GridView1.DataSource = list;
            //调用绑定数据的方法DataBind()
            GridView1.DataBind();

        }
    }

    /// <summary>
    /// 数据库实体类
    /// </summary>
    public class Catagory
    {
        private int categoryID;

        public int CategoryID
        {
            get { return categoryID; }
            set { categoryID = value; }
        }
        private string _desn;

        public string Desn
        {
            get { return Desn; }
            set { _desn = value; }
        }
        private int parentID;

        public int ParentID
        {
            get { return parentID; }
            set { parentID = value; }
        }
        private int orderby;

        public int OrderBy
        {
            get { return orderby; }
            set { orderby = value; }
        }
        private string remark;

        public string Remark
        {
            get { return remark; }
            set { remark = value; }
        }


    }

}